package com.android.wellcherish.workclock.bean.db;

import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Embedded;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * bean 类，存储打卡记录，与数据库的表映射
 *
 * @author 张文琛
 * @date 2019/11/1 20:49
 */
@Entity(tableName = RecordConstant.TABLE_NAME,
    primaryKeys = {RecordConstant.PRIMARY_TIME_COLUMN_ANME,
        RecordConstant.SECONDARY_TIME_COLUMN_ANME})
public class Record {
    @PrimaryKey(autoGenerate = true)
    private long id;
    /**
     * 打卡时的秒数，其值等于 Unix 时间戳代表的毫秒数除以 1000
     * */
    @ColumnInfo(name = RecordConstant.PRIMARY_TIME_COLUMN_ANME)
    private long clickSecond;
    /**
     * 在当前1秒内的纳秒数 1s = 1,000,000,000ns，该属性和{@link #clickSecond}
     * 一起确定唯一的一个时间戳
     *
     * 采用 int 型存储，完全可以满足纳秒数的范围
     * */
    @ColumnInfo(name = RecordConstant.SECONDARY_TIME_COLUMN_ANME)
    private int nanoInSecond;

    @Embedded
    private Date date;

    public Record() {
    }

    /**
     * 构造方法要添加 Ignore，意思是不映射数据库
     * */
    @Ignore
    public Record(long clickSecond, int nanoInSecond) {
        this.clickSecond = clickSecond;
        this.nanoInSecond = nanoInSecond;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getClickSecond() {
        return clickSecond;
    }

    public void setClickSecond(long clickSecond) {
        this.clickSecond = clickSecond;
    }

    public int getNanoInSecond() {
        return nanoInSecond;
    }

    public void setNanoInSecond(int nanoInSecond) {
        this.nanoInSecond = nanoInSecond;
    }

    @Ignore
    @NonNull
    @Override
    public String toString() {
        return "Record{" +
            "id=" + id +
            ", clickSecond=" + clickSecond +
            ", nanoInSecond=" + nanoInSecond +
            '}';
    }

    @Override public boolean equals(@Nullable Object obj) {
        if(obj == null || obj.getClass() != getClass()) {
            return false;
        }
        if(this == obj) {
            return true;
        }
        Record that = (Record) obj;

        return this.getId() == that.getId();
    }

    @Override public int hashCode() {
        int result = 17;
        result = 31 * result + Long.hashCode(id);
        result = 31 * result + Long.hashCode(clickSecond);
        result = 31 * result + Integer.hashCode(nanoInSecond);

        return result;
    }
}

